#include "queue.h"
//队列的初始化
void QueueInit(Queue* pq)
{
    assert(pq);
    pq->phead = pq->ptail = NULL;
    pq->size = 0;
}
//入队列,队尾
void QueuePush(Queue* pq,QUDataType x)
{
    assert(pq);
    QNode* newnode = (QNode*)malloc(sizeof(QNode)); 
    if(newnode == NULL)
    {
        perror("malloc error");
        exit(1);
    }
    newnode->data = x;
    newnode->next = NULL;
    if(pq->phead == NULL)
    {
        pq->phead = pq->ptail = newnode;
    }
    else
    {
        pq->ptail->next = newnode;
        pq->ptail = newnode;
    }
    pq->size++;
}
//队列的判空
bool QueueEmpty(Queue* pq)
{
    assert(pq);
    return !pq->size;
}
//出队列
void QueuePop(Queue* pq)
{
    assert(pq && !QueueEmpty(pq));
    if(pq->phead == pq->ptail)
    {
        free(pq->phead);
        pq->phead = pq->ptail = NULL;
    }
    else
    {
        QNode* del = pq->phead;
        pq->phead = pq->phead->next;
        free(del);
        del = NULL;
    }
    pq->size--;
}
//销毁队列
void QueueDestroy(Queue* pq)
{
    assert(pq);
    if(!QueueEmpty(pq))
    {
        QNode* next = pq->phead;
        QNode* pcur = pq->phead;
        while(pcur)
        {
            next = pcur->next;
            free(pcur);
            pcur = next;
        }
        pq->phead = pq->ptail = NULL;
        pq->size = 0;
    }
}
//取队尾的元素
QUDataType QueueBack(Queue* pq)
{
    assert(pq && !QueueEmpty(pq));
    return pq->ptail->data;
}
//取队头的元素
QUDataType QueueFront(Queue* pq)
{
    assert(pq && !QueueEmpty(pq));
    return pq->phead->data;
}
//队列有效元素个数
int QueueSize(Queue* pq)
{
    assert(pq);
    return pq->size;
}
